package org.dftproject.lineagelinkage.ui.pages;

import java.net.URL;

import org.dftproject.genesis.core.agents.AgentManager;
import org.dftproject.genesis.core.collections.Collection;
import org.dftproject.genesis.core.collections.CollectionManager;
import org.dftproject.genesis.ui.Activator;
import org.dftproject.genesis.ui.agents.pages.AgentsPage;
import org.dftproject.genesis.ui.agents.pages.AgentsPageInput;
import org.dftproject.genesis.ui.figures.FigureCanvasEx;
import org.dftproject.genesis.ui.figures.HyperlinkFigure;
import org.dftproject.genesis.ui.figures.IHyperlinkListener;
import org.dftproject.genesis.ui.figures.PageContainer;
import org.dftproject.genesis.ui.pages.AbstractPagePart;
import org.dftproject.genesis.ui.pages.IPageInput;
import org.dftproject.genesis.ui.pages.IPageSite;
import org.dftproject.genesis.ui.pages.PageInitException;
import org.dftproject.genesis.ui.pages.PageManager;
import org.dftproject.genesis.ui.pages.PageUtils;
import org.dftproject.lineagelinkage.core.agents.FindDuplicatesAgent;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

public class NewFindDuplicatesAgentPage extends AbstractPagePart {

	public static final String ID = NewFindDuplicatesAgentPage.class.getName();

	private FigureCanvasEx canvas;

	public void init(IPageSite site, IPageInput input) throws PageInitException {
		this.setSite(site);
		this.setInput(input);
	}

	@Override
	public void createPartControl(Composite parent) {
		createContents(parent);
	}

	protected void createContents(Composite parent) {
		// Create the main Draw2D canvas

		canvas = new FigureCanvasEx(parent);
		canvas.getViewport().setContentsTracksWidth(true);

		// Create the root figure

		PageContainer container = new PageContainer();
		canvas.setContents(container);

		container.add(PageUtils.createH1Label("Find Potential Duplicate Individuals"));

		container.add(PageUtils.createFlowedText("Select a collection to analyze:"));
		
		IFigure collectionList = new Figure();
		ToolbarLayout layout = new ToolbarLayout(false);
		layout.setSpacing(10);
		layout.setStretchMinorAxis(false);
		collectionList.setLayoutManager(layout);
		collectionList.setBorder(new MarginBorder(10, 10, 0, 0));
		container.add(collectionList);
		
		for (final Collection collection : CollectionManager.getDefault().getRecentCollections(0)) {
			HyperlinkFigure link = new HyperlinkFigure(collection.getName());
			Image icon = getCollectionIcon(collection);
			if (icon != null)
				link.setIcon(icon);
			link.addHyperlinkListener(new IHyperlinkListener() {

				public void linkActivated(Object data, int stateMask) {
					AgentManager.getDefault().addAgent(new FindDuplicatesAgent(collection));
					
					IWorkbench workbench = PlatformUI.getWorkbench();
					IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
					IWorkbenchPage workbenchPage = window.getActivePage();
					PageManager.getDefault().openPage(workbenchPage, new AgentsPageInput(), AgentsPage.ID, true, (stateMask & SWT.CTRL) != 0);
				}
				
			});
			collectionList.add(link);
		}
	}
	
	protected static Image getCollectionIcon(Collection collection) {
		String type = collection.getType();

		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint extensionPoint = registry.getExtensionPoint(Activator.ID, Activator.COLLECTION_HANDLERS_EXTENSION_POINT);
		IExtension[] extensions = extensionPoint.getExtensions();
		for (IExtension extension : extensions) {
			IConfigurationElement[] configElements = extension.getConfigurationElements();
			for (IConfigurationElement configElement : configElements) {
				if (type.equals(configElement.getAttribute("type"))) {
					String iconPath = configElement.getAttribute("icon");
					String plugin = extension.getNamespaceIdentifier();
					return getImageFromPlugin(plugin, iconPath);
				}
			}
		}

		return null;
	}

	protected static Image getImageFromPlugin(String plugin, String iconPath) {		
		Image image = null;
		try {
			ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(new URL(Platform.getBundle(plugin).getEntry("/"), iconPath));
			image = imageDescriptor.createImage();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return image;
	}

	@Override
	public void setFocus() {
		canvas.setFocus();
	}

}
